package cn.kepu.elearning.dto.match;

import cn.kepu.elearning.common.Constants;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Endy
 * @create 2019-12-12 上午9:12
 **/
@Getter
@Setter
@ToString(callSuper = true)
public class MatchQueryNoticeDTO extends CommonQueryDTO {

    private String tenantId;
    private String ifTenantId;

    public MatchQueryDTO convert() {
        MatchQueryDTO matchQueryDTO = new MatchQueryDTO();
        matchQueryDTO.setType(Constants.Type.NOTICE);
        matchQueryDTO.setPageNum(this.getPageNum());
        matchQueryDTO.setPageSize(this.getPageSize());
        matchQueryDTO.setFilterText(this.getFilterText());
        matchQueryDTO.setFieldNames(this.getFieldNames());
        matchQueryDTO.setOther(generatorOther());
        return matchQueryDTO;
    }

    private List<OtherQueryDTO> generatorOther() {
        List<OtherQueryDTO> other = new ArrayList<>();
        OtherQueryDTO other1 = new OtherQueryDTO();
        other1.setLogicOperator(Constants.LogicOperator.MUST);

        List<OtherQueryDTO> subOther = new ArrayList<>();
        subOther.add(generatorSubOther1());
        subOther.add(generatorSubOther2());
        subOther.add(generatorSubOther3());
        other1.setSubOther(subOther);

        other.add(other1);

        if (StringUtils.isNotBlank(this.getIfTenantId()) && StringUtils.isNotBlank(this.getTenantId())) {
            if ("2701".equals(this.getIfTenantId())) {
                OtherQueryDTO other5 = new OtherQueryDTO();
                other5.setField("tenantId");
                other5.setFieldType("number");
                other5.setValue(this.getTenantId());
                other5.setOperator(Constants.Operator.TERM);
                other5.setLogicOperator(Constants.LogicOperator.MUST);
                other.add(other5);
            } else if ("2702".equals(this.getIfTenantId())) {
                OtherQueryDTO other5 = new OtherQueryDTO();
                other5.setField("tenantId");
                other5.setFieldType("number");
                other5.setValue(this.getTenantId());
                other5.setOperator(Constants.Operator.TERM);
                other5.setLogicOperator(Constants.LogicOperator.MUST_NOT);
                other.add(other5);
            }
        }
        return other;
    }

    private OtherQueryDTO generatorSubOther1() {
        OtherQueryDTO subOther1 = new OtherQueryDTO();
        subOther1.setLogicOperator(Constants.LogicOperator.SHOULD);
        List<OtherQueryDTO> subSubOther = new ArrayList<>();
        OtherQueryDTO subSubOther1 = new OtherQueryDTO();
        subSubOther1.setField("isPublic");
        subSubOther1.setFieldType("number");
        subSubOther1.setValue("1153");
        subSubOther1.setOperator(Constants.Operator.TERM);
        subSubOther1.setLogicOperator(Constants.LogicOperator.MUST);
        subSubOther.add(subSubOther1);
        OtherQueryDTO subSubOther2 = new OtherQueryDTO();
        subSubOther2.setField("hasSend");
        subSubOther2.setFieldType("number");
        subSubOther2.setValue("1");
        subSubOther2.setOperator(Constants.Operator.TERM);
        subSubOther2.setLogicOperator(Constants.LogicOperator.MUST);
        subSubOther.add(subSubOther2);
        OtherQueryDTO subSubOther3 = new OtherQueryDTO();
        subSubOther3.setField("STATUS");
        subSubOther3.setFieldType("number");
        subSubOther3.setValue("1092");
        subSubOther3.setOperator(Constants.Operator.TERM);
        subSubOther3.setLogicOperator(Constants.LogicOperator.MUST);
        subSubOther.add(subSubOther3);
        subOther1.setSubOther(subSubOther);
        return subOther1;
    }

    private OtherQueryDTO generatorSubOther2() {
        OtherQueryDTO subOther2 = new OtherQueryDTO();
        subOther2.setLogicOperator(Constants.LogicOperator.SHOULD);
        List<OtherQueryDTO> subSubOther = new ArrayList<>();
        OtherQueryDTO subSubOther1 = new OtherQueryDTO();
        subSubOther1.setField("isPublic");
        subSubOther1.setFieldType("number");
        subSubOther1.setValue("1156");
        subSubOther1.setOperator(Constants.Operator.TERM);
        subSubOther1.setLogicOperator(Constants.LogicOperator.MUST);
        subSubOther.add(subSubOther1);
        OtherQueryDTO subSubOther2 = new OtherQueryDTO();
        subSubOther2.setField("recommendTag");
        subSubOther2.setFieldType("number");
        subSubOther2.setValue("1");
        subSubOther2.setOperator(Constants.Operator.TERM);
        subSubOther2.setLogicOperator(Constants.LogicOperator.MUST);
        subSubOther.add(subSubOther2);
        subOther2.setSubOther(subSubOther);
        return subOther2;
    }

    private OtherQueryDTO generatorSubOther3() {
        OtherQueryDTO subOther3 = new OtherQueryDTO();
        subOther3.setLogicOperator(Constants.LogicOperator.SHOULD);
        List<OtherQueryDTO> subSubOther = new ArrayList<>();
        OtherQueryDTO subSubOther1 = new OtherQueryDTO();
        subSubOther1.setField("isPublic");
        subSubOther1.setFieldType("number");
        subSubOther1.setValue("1157");
        subSubOther1.setOperator(Constants.Operator.TERM);
        subSubOther1.setLogicOperator(Constants.LogicOperator.MUST);
        subSubOther.add(subSubOther1);
        OtherQueryDTO subSubOther2 = new OtherQueryDTO();
        subSubOther2.setField("recommendTag");
        subSubOther2.setFieldType("number");
        subSubOther2.setValue("1");
        subSubOther2.setOperator(Constants.Operator.TERM);
        subSubOther2.setLogicOperator(Constants.LogicOperator.MUST);
        subSubOther.add(subSubOther2);
        subOther3.setSubOther(subSubOther);
        return subOther3;
    }
}
